package com.leenleda.ips.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.leenleda.ips.R;
import com.leenleda.ips.databinding.DialogDownloadBinding;
import com.leenleda.ips.entity.MediaEntity;
import com.leenleda.ips.net.NetInterface;
import com.leenleda.ips.receiver.Constant;
import com.leenleda.ips.util.FileUtils;
import com.leenleda.ips.util.LogUtils;

public class DialogDownload {
    private Dialog dialog;
    private DialogDownloadBinding binding;
    private OnClickListener listener;
    private Context mContext;
    private String tag = "DialogDownload";

    public DialogDownload(Context context) {
        mContext = context;
        binding = DialogDownloadBinding.inflate(LayoutInflater.from(mContext));
        dialog = new Dialog(mContext, R.style.black_dialog);// 创建自定义样式dialog

        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        window.setAttributes(params);
        window.setGravity(Gravity.CENTER);

        dialog.setCancelable(false);// 可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));// 设置布局
        dialog.setOnDismissListener(dialog -> {
            LogUtils.e(tag,"---onDismiss-*********************************************--: ");
            if (listener != null)
                listener.onDismiss();
        });
    }

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void show(MediaEntity entity) {//"http://192.168.1.138:1301/path/file/materialFile/20201222134816/56/video_list.m3u8"
        binding.progress.setMaxValue(100);
        binding.progress.setValue(0);
        NetInterface.GetInstance().downLoad(entity.getFilepath(), FileUtils.getPath(Constant.CACHE_FOLDER), System.currentTimeMillis() + "_" + entity.getId() + ".mp4");

//        Log.e(tag, "=====entity======" + entity.getFilepath());
//        DownloadManager.getInstance().download(entity.getFilepath() + "/Aest.mp4", "Aest.mp4");
////        DownloadManager.getInstance().download("http://106.12.156.127:8580/down/VV3pyqYktb7W");
        try {
            if (!dialog.isShowing()) {
                dialog.show();
            }
        } catch (Exception e) {
        }
    }

    public void dismiss() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void update(int progress){
        binding.progress.setValue(progress);
//        if (progress == 100)
//            listener.onDismiss();
    }

    public interface OnClickListener {
        void onDismiss();//点击列表监听
    }


//    ===***下载***====


}
